package smedi.ismedi.web.bff.controller.safetyeducation;

import com.smedi.ismedi.file.dto.response.FileInfoResponse;
import com.smedi.ismedi.file.feign.RemoteFileFeignApi;
import com.smedi.ismedi.safety.education.api.dto.request.SpecialTrainingQueryRequest;
import com.smedi.ismedi.safety.education.api.dto.request.SpecialTrainingUpdateRequest;
import com.smedi.ismedi.safety.education.api.dto.response.PaginationResponse;
import com.smedi.ismedi.safety.education.api.dto.response.SpecialTrainingResponse;
import com.smedi.ismedi.safety.education.api.feign.RemoteSpecialTrainingFeignApi;
import com.smedi.ismedi.web.starter.common.ResponseData;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.cloud.openfeign.SpringQueryMap;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import smedi.ismedi.web.bff.controller.BaseController;
import smedi.ismedi.web.bff.infrastructure.util.FileHelper;

import javax.servlet.http.HttpServletResponse;
import java.util.List;

@Slf4j
@RestController
@RequestMapping("/webbff/v1/specialTraining")
@RequiredArgsConstructor
@Api(tags = "专项培训API")
public class SpecialTrainingController extends BaseController {
    private final RemoteSpecialTrainingFeignApi remoteSpecialTrainingFeignApi;
    private final RemoteFileFeignApi fileFeignApi;

    @Value("${fdfs.service.address.in}")
    private String FDFS_SERVICE_ADDRESS_IN;

    @Value("${fdfs.service.address.out}")
    private String FDFS_SERVICE_ADDRESS_OUT;


    /**
     * 创建专项培训
     *
     * @param specialTrainingUpdateRequest
     * @return
     */
    @ApiOperation(httpMethod = "POST", value = "创建专项培训")
    @PostMapping(value = "/saveSpecialTraining")
    public ResponseData<SpecialTrainingResponse> postSaveSpecialTraining(@Validated @RequestBody SpecialTrainingUpdateRequest specialTrainingUpdateRequest) {
        return remoteSpecialTrainingFeignApi.postSaveSpecialTraining(specialTrainingUpdateRequest);
    }

    /**
     * 删除专项培训
     *
     * @param ids
     * @return
     */
    @ApiOperation(httpMethod = "DELETE", value = "删除专项培训")
    @DeleteMapping(value = "/deleteSpecialTraining")
    public ResponseData deleteSpecialTraining(@Validated @RequestBody List<Long> ids) {
        return remoteSpecialTrainingFeignApi.deleteSpecialTraining(ids);
    }

    /**
     * 查询专项培训
     *
     * @param specialTrainingQueryRequest
     * @return
     */
    @ApiOperation(httpMethod = "GET", value = "查询专项培训")
    @GetMapping(value = "/querySpecialTraining")
    public ResponseData<PaginationResponse> getQueryAllSpecialTraining(@Validated @SpringQueryMap SpecialTrainingQueryRequest specialTrainingQueryRequest) {
        return remoteSpecialTrainingFeignApi.getQueryAllSpecialTraining(specialTrainingQueryRequest);
    }

    @ApiOperation(httpMethod = "GET", value = "查询单个专项培训信息")
    @GetMapping(value = "/querySpecialTrainingInfo")
    public ResponseData<SpecialTrainingResponse> getSpecialTrainingInfo(@RequestParam("id") Long id) {
        return remoteSpecialTrainingFeignApi.getSpecialTrainingInfo(id);
    }

    @ApiOperation(httpMethod = "GET", value = "预览单个专项培训信息")
    @GetMapping(value = "/previewSpecialTrainingInfo")
    public ResponseData<String> previewSpecialTraining(@RequestParam("id") Long id, HttpServletResponse response) {
        String signKey = remoteSpecialTrainingFeignApi.exportSpecialTraining(id).getData();
        FileInfoResponse fileInfoResponse = fileFeignApi.getFileInfo(signKey).getData();
        String url = FileHelper.getUrl(FDFS_SERVICE_ADDRESS_OUT, fileInfoResponse.getStorageGroup(), fileInfoResponse.getPath(), fileInfoResponse.getName(), response);

        return new ResponseData<>().success(url);
    }

    @ApiOperation(httpMethod = "POST", value = "预览多个专项培训信息")
    @PostMapping(value = "/previewSpecialTrainingBatch")
    public ResponseData<String> previewSpecialTrainingBatch(@Validated @RequestBody List<Long> ids, HttpServletResponse response) {
        String signKey = remoteSpecialTrainingFeignApi.exportSpecialTrainingBatch(ids).getData();
        FileInfoResponse fileInfoResponse = fileFeignApi.getFileInfo(signKey).getData();
        String url = FileHelper.getUrl(FDFS_SERVICE_ADDRESS_OUT, fileInfoResponse.getStorageGroup(), fileInfoResponse.getPath(), fileInfoResponse.getName(), response);

        return new ResponseData<>().success(url);
    }

    @ApiOperation(httpMethod = "GET", value = "导出单个专项培训信息")
    @GetMapping(value = "/exportSpecialTrainingInfo")
    public ResponseData<Boolean> exportSpecialTraining(@RequestParam("id") Long id, HttpServletResponse response) {
        String signKey = remoteSpecialTrainingFeignApi.exportSpecialTraining(id).getData();
        FileInfoResponse fileInfoResponse = fileFeignApi.getFileInfo(signKey).getData();
        FileHelper.downloadFile(FDFS_SERVICE_ADDRESS_IN, fileInfoResponse.getStorageGroup(), fileInfoResponse.getPath(), fileInfoResponse.getName(), response);

        return new ResponseData<>().success(true);
    }

    @ApiOperation(httpMethod = "POST", value = "导出多个专项培训信息")
    @PostMapping(value = "/exportSpecialTrainingBatch")
    public ResponseData<Boolean> exportSpecialTrainingBatch(@Validated @RequestBody List<Long> ids, HttpServletResponse response) {
        String signKey = remoteSpecialTrainingFeignApi.exportSpecialTrainingBatch(ids).getData();
        FileInfoResponse fileInfoResponse = fileFeignApi.getFileInfo(signKey).getData();
        FileHelper.downloadFile(FDFS_SERVICE_ADDRESS_IN, fileInfoResponse.getStorageGroup(), fileInfoResponse.getPath(), fileInfoResponse.getName(), response);

        return new ResponseData<>().success(true);
    }
}
